package DynamicProgramingSeries;

public class NumSuqaresSolution_279 {
    public int numSquares(int n) {
        // 完全背包问题
        // dp[j] = 平方和为j的最少数字组合
        int[] dp = new int[n + 1];

        dp[0] = 0;
        for (int j = 1; j <= n; j++) {
            dp[j] = Integer.MAX_VALUE;
        }

        for (int i = 1; i * i <= n; i++) {
            for (int j = i * i; j <= n; j++) {
                dp[j] = Math.min(dp[j - i * i] + 1, dp[j]);
            }
        }
        return dp[n];
    }
}
